package com.mock.ems.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MOCK on 2017/7/8.
 */
public class ResourceServiceImpl implements  ResourceService {

    @Override
    public List<Map<String,Object>> getMenuListByUserId(Long userId) {

        //获取属于用户的第一层菜单(level为1，type为1)
        String querySQL = " select * from ems_menu " +
                " where type = 1 and level = 1 and id in (select menu_id from ems_admin_menu where admin_id = ? )" +
                " order by display_order asc ";

        List<Record> menuList0 = Db.find(querySQL,userId);
        if (CollectionUtil.isEmpty(menuList0)) return new ArrayList<>();

        //查询一级菜单下面的子菜单
        List<Map<String,Object>> returnList = new ArrayList<>();
        for (Record parent : menuList0) {
            returnList.add(recurseMenu(parent,userId));
        }

        return returnList;
    }


    private Map<String,Object> recurseMenu(Record parent0,Long userId) {
        Long parentId = parent0.getLong("id");
        String sql = "select * from ems_menu where parent_id = ? " +
        "and id in ("
        + "select menu_id from ems_admin_menu where admin_id = ? )";
        List<Record> childList = Db.find(sql, parentId,userId);
        List<Map<String,Object>> returnList = new ArrayList<>();
        if (CollectionUtil.isEmpty(childList)) {
            Map<String,Object> menuMap = record2Map(parent0);
            menuMap.put("children", returnList);
            return menuMap;
        }else {
            Map<String,Object> menuMap = record2Map(parent0);
            for (Record parent : childList) {
                returnList.add(recurseMenu(parent,userId));
            }
            menuMap.put("children", returnList);
            return menuMap;
        }
    }

    private Map<String,Object> record2Map(Record record) {

        Map<String,Object> menuMap = new HashMap<>();
        menuMap.put("id", record.get("id"));
        menuMap.put("name", record.get("name"));
        menuMap.put("url", record.get("url"));
        menuMap.put("level", record.get("level"));
        menuMap.put("type", record.get("type"));
        menuMap.put("pId", record.get("parent_id"));
        return menuMap;
    }

    @Override
    public void deteleMenuByMenuId(Long menuId) {
        //删除菜单表中的数据
        Db.deleteById("ems_menu",menuId);

        //删除ems_admin_user_menu表中的数据
        Db.update("delete from ems_role_menu where menu_id = ? ", menuId);

    }


    @Override
    public List<Map<String, Object>> getMenuListByRoleId(Long roleId) {
        //获取属于用户的第一层菜单(level为1，type为1)
        String querySQL = " select * from ems_menu " +
                " where id in " +
                " (select menu_id from ems_role_menu where role_id = ? ) " +
                " and type = 1 and level = 1 order by display_order asc ";

        List<Record> menuList0 = Db.find(querySQL, roleId);
        if (CollectionUtil.isEmpty(menuList0)) return new ArrayList<>();

        //查询一级菜单下面的子菜单
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Record parent : menuList0) {
            resultList.add(recurseRoleMenu(parent, roleId));
        }

        return resultList;
    }

    private Map<String,Object> recurseRoleMenu(Record parent0,Long roleId) {
        Long parentId = parent0.getLong("id");
        String sql = "select * from ems_menu where id in ("
                + "select menu_id from ems_role_menu where role_id = ? ) and parent_id = ? order by display_order asc";
        List<Record> childList = Db.find(sql,roleId,parentId);
        List<Map<String,Object>> returnList = new ArrayList<>();
        if (CollectionUtil.isEmpty(childList)) {
            Map<String,Object> menuMap = record2Map(parent0);
            //menuMap.put("nodes", returnList);
            return menuMap;
        }else {
            Map<String,Object> menuMap = record2Map(parent0);
            for (Record parent : childList) {
                returnList.add(recurseRoleMenu(parent,roleId));
            }
            menuMap.put("nodes", returnList);
            return menuMap;
        }
    }
}
